package com.lz.sports.widget

import android.content.Context
import android.util.AttributeSet
import android.widget.LinearLayout
import com.lz.library.extension.APP
import com.lz.library.extension.getScreenPoint
import com.lz.sports.R

class ScreenLinearLayout : LinearLayout {

    private var screenReduce = 0

    constructor(context: Context) : super(context) {
        init(context, null)
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        init(context, attrs)
    }

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        init(context, attrs)
    }

    private fun init(context: Context, attrs: AttributeSet?) {
        if (attrs != null) {
            val typedArray = context.obtainStyledAttributes(attrs, R.styleable.ScreenLinearLayout)
            screenReduce = typedArray.getDimensionPixelOffset(R.styleable.ScreenLinearLayout_screenReduce, screenReduce)
            typedArray.recycle()
        }
        orientation = HORIZONTAL
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        val width = APP.getScreenPoint().x - screenReduce
        super.onMeasure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), heightMeasureSpec)
    }
}